package cn.edu.lzzy.handler;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 自定义统一处理类
 */
@ControllerAdvice
public class MyExceptionHandler {
    public ModelAndView businessExceptionHandler(HttpServletRequest request,BusinessException e){
        String errorInfo="业务异常"+e.getErrorInfo();
        ModelAndView mv = new ModelAndView();
        mv.addObject("title",errorInfo);
        mv.addObject("url",
                request.getRequestURI().toString());
        mv.addObject("exception",e);
        mv.setViewName("error/error");
        return mv;
    }
    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandler(HttpServletRequest request,BusinessException e) throws Exception{
    if(AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class)!=null){
        throw e;
    }
        ModelAndView mv = new ModelAndView();
        mv.addObject("title","非业务异常");
        mv.addObject("url",
                request.getRequestURI().toString());
        mv.addObject("exception",e);
        mv.setViewName("error/error");
        return mv;
    }
}
